#ifndef __RS485O_H__
#define __RS485O_H__

#include <Config.h>
#include <stdlib.h>
#include <string.h>


/*Structure with communication variable */
/* used for sending /receiving packet   */
/* Here only variable that are not phisically sent over the line */

typedef struct CommProt {
    WORD_VAL cOurAddr;      /* Our address                          */
    //char cRs485RxChar;      /* last received char                   */
    int cRS485State;        /* state of receiveing                  */
    WORD_VAL cCalcCrc;      /*  Our Calculated  Crc byte            */
    char cBufPtr;           /*  Pointer in Buffer                   */
    char cError;            /*  Packet  Error                       */

} CommProt;

typedef struct RxCommPack {
    char cStart;            /* Start char of message                */
    WORD_VAL cNetAddr;
    char cLenExpected;      /* Expected length of message           */
    char cCommand;          /* Received Command                     */
    char c485Buf[64];       /* Data Buffer of cLenExpected length   */
    WORD_VAL cRxCrc;        /*  Actual received CRC byte            */
} RxCommPack;

typedef struct TxCommPack {
    char cStart;
    WORD_VAL cNetAddr;
    char cLenExpected;
    char cCommand;
    char c485Buf[64];
    WORD_VAL cCalcCrc;
} TxCommPack;

enum Rs485Flag {
    SER_REC = 0x01,
    SER_TX =  0x02
} ;


extern char  strPacket[];
extern OST_MSG drv_rs485_msg_rx;
extern OST_MSG drv_rs485_msg_tx;
extern OST_QMSG DRV_rs485_qmsg_rx;
extern OST_QMSG DRV_rs485_qmsg_tx;
extern OST_MSG DRV_rs485_msg_q_tx[RS_SIZE_OF_MSGQ];
extern OST_MSG DRV_rs485_msg_q_rx[RS_SIZE_OF_MSGQ];
extern OST_FLAG Rs485Flag;

//BOOL Rs485Process(RxCommPack *cmptr);
extern void Rs485Initialise(WORD_VAL, CommProt *);
extern char Rs485Decode(RxCommPack *,CommProt *, char);

extern void Rs485UpdateCrc(char cVal);
extern void CRC16_Init(void);
extern void CRC16_Update4Bits(char val);
extern void Rs485SendPacket(TxCommPack *);

extern void Rs485SendChar(char c);
extern char PostValidatePacket(RxCommPack *commprt);
extern char PacketHasPayload(void);
extern void BinToHexAscii(char c);
extern char *MakeStr(char *cData, char cLen, char cCmd);


#define NUM_TX_PREAMBLE 3


// Protocol defines
#define PKT_START 0x02

// States of packet decoder state machine
#define PKT_WAIT_START      0
#define PKT_WAIT_ADDR_HIGH  1
#define PKT_WAIT_ADDR_LOW   2
#define PKT_WAIT_LEN        3
#define PKT_CMD             4
#define PKT_WAIT_DATA       5
#define PKT_WAIT_CRC_HIGH   6
#define PKT_WAIT_CRC_LOW    7
#define PKT_COMPLETE        8
#define PKT_VALID           9
#define PKT_INVALID       255

// Error codes
#define BAD_LENGTH 1
#define BAD_CRC    2


// Packet types
#define SENSOR_POLL_CMD        0xA1
#define SENSOR_PING_SYNC       0xA2
#define SENSOR_CONFIG          0xA3
#define SENSOR_GET_DATA        0xA4



#define SENSOR_ACK             0xB0
#define SENSOR_NAK             0xC0

#endif

